package com.qf.smsplatform.service.Impl;
/*2021/10/29*/

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.smsplatform.cache.TNotifyCache;
import com.qf.smsplatform.constans.ResultCode;
import com.qf.smsplatform.events.UpdateTNotifyEvent;
import com.qf.smsplatform.exception.AddDataException;
import com.qf.smsplatform.exception.DeleteDataException;
import com.qf.smsplatform.exception.QueryDataException;
import com.qf.smsplatform.exception.UpdateDataException;
import com.qf.smsplatform.mapper.TNotifyMapper;
import com.qf.smsplatform.pojo.CheckType;
import com.qf.smsplatform.pojo.MyAuthUser;
import com.qf.smsplatform.pojo.TNotify;
import com.qf.smsplatform.service.TNotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @YuTao
 */
@Service
@Transactional
public class TNotifyServiceImpl implements TNotifyService {

    private TNotifyMapper notifyMapper;
    @Autowired
    public void setNotifyMapper(TNotifyMapper notifyMapper) {
        this.notifyMapper = notifyMapper;
    }

    private ApplicationContext context;
    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    private TNotifyCache notifyCache;
    @Autowired
    public void setNotifyCache(TNotifyCache notifyCache) {
        this.notifyCache = notifyCache;
    }

    @Override
    public void updateNotify(TNotify notify) {
        //从缓存中拿到当前需要删除的缓存开关数据，判断是否存在
//        TNotify tNotifyByKey = notifyCache.getTNotifyByKey(notify.getTag());
//        if(ObjectUtils.isEmpty(tNotifyByKey)){
//            throw new QueryDataException("没有查询到当前表的缓存开关", ResultCode.DATA_ISNULL);
//        }
        //拿到当前登录用户的信息，如果不是管理员，则没有权限进行修改
        MyAuthUser user = (MyAuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if(user.getType()==2){
            throw new DeleteDataException("当前用户没有权限进行修改", ResultCode.NOT_ALLOW);
        }
        //判断传递的数据是否合法
        if(notify.isNull(CheckType.UPDATE)){
            throw new UpdateDataException("传递的修改数据不合法", ResultCode.DATA_ISNULL);
        }
        notifyMapper.updateNOtify(notify);

        //更新完数据之后，发布一个事件
        context.publishEvent( new UpdateTNotifyEvent());
    }

    @Override
    public void deleteTNotify(String tag) {
        //从缓存中拿到当前需要删除的缓存开关数据，判断是否有权限进行删除
        TNotify tNotifyByKey = notifyCache.getTNotifyByKey(tag);
        if(ObjectUtils.isEmpty(tNotifyByKey)){
            throw new QueryDataException("当前表没有设置缓存开关", ResultCode.DATA_ISNULL);
        }
        //获取当前登录用户信息
        MyAuthUser user = (MyAuthUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(user.getType()==2){//只有管理员才能进行数据的删除
            throw new DeleteDataException("当前用户没有权限进行缓存表的信息删除", ResultCode.NOT_ALLOW);
        }
        notifyMapper.deleteTNotify(tag);//调用逻辑层，进行数据的删除
        //删除信息后，进行缓存的更新
        context.publishEvent(new UpdateTNotifyEvent());
    }

    @Override
    public void insertTNotify(TNotify notify) {
        if(notify.isNull(CheckType.ADD)){
            throw new AddDataException("传递的数据不合法", ResultCode.DATA_ISNULL);
        }
        //调用持久层，进行数据的添加
        notifyMapper.insertTNotify(notify);
        //发布事件，进行缓存的更新
        context.publishEvent(new UpdateTNotifyEvent());
    }

    @Override
    public PageInfo<TNotify> selectPage(int pageNum, int pageSize) {
        //获取当前登录用户的信息，判断那些数据能够看到，那些数据无法看到
        MyAuthUser user = (MyAuthUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //限制查询的数据量
        PageHelper.startPage(pageNum, pageSize);
        List<TNotify> allData = notifyCache.getAllData();
//        //对查询出来的信息进行具体的判断
//        if(user.isNull(CheckType.DELETE)){
//            throw new QueryDataException("没有权限进行信息查询", ResultCode.NOT_ALLOW);
//        }
        List<TNotify> collect = allData.stream()
                .skip((pageNum - 1) * pageSize)
                .limit(pageSize)
                .collect(Collectors.toList());
        PageInfo<TNotify> pageInfo=new PageInfo<>(collect);

        return pageInfo;
    }


}
